package com.itheima.config;


import com.itheima.utils.ServletUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;

@Component
public class ServerConfig {

    /**
     * 获取完整的请求路径，包括：域名，端口，上下文访问路径
     *
     * @return 服务地址
     */
    public String getUrl()
    {
        HttpServletRequest request = ServletUtils.getRequest();
        return getDomain(request);
    }

    public static String getDomain(HttpServletRequest request)
    {
        //  获取请求的协议（http、https）和域名
        StringBuffer url = request.getRequestURL();
        //  获取项目上下文路径
        String contextPath = request.getServletContext().getContextPath();
        //  删除 URI 长度部分,拼接完整的请求路径
        return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString();
    }
}
